//
//  ProjectDocument.m
//  iCodeMac
//
//  Created by Andrew Barry on 1/04/09.
//  Copyright 2009 Barry Computer Services. All rights reserved.
//

#import "ProjectDocument.h"
#import "JavascriptProject.h"
#import "ProjectWindowController.h"

@implementation ProjectDocument

-(BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem {

	SEL theAction = [anItem action];
	JavascriptProject *project = [JavascriptProject projectForDocument: self];

	if (theAction == @selector(addNewFile:) ||
		theAction == @selector(addNewForm:) ||
		theAction == @selector(projectProperties:) ||
		theAction == @selector(addExistingFile:)) {
		return project != nil;
	}
	if (theAction == @selector(debugRun:) ||
		theAction == @selector(buildApplication:)) {
		return project != nil;
	}
	if (theAction == @selector(debugStop:)) {
		return project != nil && [project isDebugging];
	}
	if (theAction == @selector(stepInto:) ||
		theAction == @selector(stepOver:) ||
		theAction == @selector(stepOut:)) {
		return project != nil && [project isDebugging] && [project isStopped];
	}
	if (theAction == @selector(home:)) {
		return project != nil;
	}

	return YES;
//	return [super validateUserInterfaceItem:anItem];
}

-(void)buildApplication: (id) sender {
	JavascriptProject *project = [JavascriptProject projectForDocument: self];
	if (project != nil)
		[project debugRun: sender];
}

-(void)debugRun: (id) sender {
	JavascriptProject *project = [JavascriptProject projectForDocument: self];
	if (project != nil)
		[project debugRun: sender];
}

-(void)debugStop: (id) sender {
	JavascriptProject *project = [JavascriptProject projectForDocument: self];
	if (project != nil)
		[project debugStop: sender];
}

-(void)stepInto: (id) sender {
	JavascriptProject *project = [JavascriptProject projectForDocument: self];
	if (project != nil)
		[project stepInto: sender];
}

-(void)stepOut: (id) sender {
	JavascriptProject *project = [JavascriptProject projectForDocument: self];
	if (project != nil)
		[project stepOut: sender];
}

-(void)stepOver: (id) sender {
	JavascriptProject *project = [JavascriptProject projectForDocument: self];
	if (project != nil)
		[project stepOver: sender];
}

-(void)home: (id) sender {
	JavascriptProject *project = [JavascriptProject projectForDocument: self];
	if (project != nil) {
		[project showWindows];
	}
}

-(void)addNewFile: (id) sender {
	JavascriptProject *project = [JavascriptProject projectForDocument: self];
	if (project != nil) {
		ProjectWindowController *controller = [[project windowControllers] objectAtIndex:0];
		[controller addNewFile: sender];
	}
}

-(void)addNewForm: (id) sender {
	JavascriptProject *project = [JavascriptProject projectForDocument: self];
	if (project != nil) {
		ProjectWindowController *controller = [[project windowControllers] objectAtIndex:0];
		[controller addNewForm: sender];
	}
}

-(void)addExistingFile: (id) sender {
	JavascriptProject *project = [JavascriptProject projectForDocument: self];
	if (project != nil) {
		ProjectWindowController *controller = [[project windowControllers] objectAtIndex:0];
		[controller addExistingFile: sender];
	}
}

-(void)projectProperties: (id) sender {
	JavascriptProject *project = [JavascriptProject projectForDocument: self];
	if (project != nil) {
		ProjectWindowController *controller = [[project windowControllers] objectAtIndex:0];
		[controller projectProperties: sender];
	}
}


@end
